/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
  if(nums.length==1){
    return nums[0]==target?0:-1
  }
  let res = -1
  let left = 0
  let mid
  let right = nums.length - 1
  // let index = Math.floor(nums.length/2)
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] > nums[i + 1]) {
      if (target > nums[i]) return -1
      if (target >= nums[left]) {
        right = i

        while (left <= right) {
          mid = Math.floor((left + right) / 2)
          if (nums[mid] == target) return mid
          else if (nums[mid] < target) {
            left = mid + 1
          } else {
            right = mid - 1
          }
        }
      } else {
        left = i + 1

        while (left <= right) {
          mid = Math.floor((left + right) / 2)
          if (nums[mid] == target) {
            return mid
          } else if (nums[mid] < target) {
            left = mid + 1
          } else {
            right = mid - 1
          }
        }
      }
    }

  }
  while(left<=right){
    mid = Math.floor((left+right)/2)
    if (nums[mid] == target) return mid
    else if (nums[mid] < target) {
      left = mid + 1
    } else {
      right = mid - 1
    }

  }
  return res
}

console.log(search([1,3], 1))
